#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <time.h>

#include "packet.h"

/* Description: Get device serial number */
int get_devsn(char *devsn, int size)
{
	memset(devsn, 0, size);
	snprintf(devsn, size , "%s", "Pi001");

	return 0;
}

/* Description: Get date/time */
int get_time(char *time, int size)
{

	struct 			tm *tm;
	struct 			timeval tv;

	gettimeofday(&tv,NULL);
	tm = localtime(&tv.tv_sec);
	snprintf(time, size, "%d-%02d-%02d %02d:%02d:%02d", 
			tm->tm_year + 1900,tm->tm_mon + 1,tm->tm_mday,
			tm->tm_hour,tm->tm_min,tm->tm_sec);//以字符串形式输出
	//printf("get_time:%s", time);
	return 0;
}

/* Description: Convert data from $pack to string format and save in $buf
   String Format: "RPI2304001,2023-04-13 20:08:08,26.66"
   */
int pack_data(packet_t *pack, char *buf, int size)
{
	memset(buf, 0, size);
	snprintf(buf, size, "%s/%s/%f\n", pack->devsn, pack->time, pack->temp);

	return 0;
}

